package cn.fxl.pet.service.impl;

import cn.fxl.basic.result.AjaxResult;
import cn.fxl.basic.service.impl.BasicServiceImpl;
import cn.fxl.basic.utils.BaiduAiUtils;
import cn.fxl.basic.utils.LoginContext;
import cn.fxl.org.domain.Employee;
import cn.fxl.org.mapper.EmployeeMapper;
import cn.fxl.pet.domain.Pet;
import cn.fxl.pet.domain.PetDetail;
import cn.fxl.pet.domain.PetOnlineAuditLog;
import cn.fxl.pet.mapper.PetDetailMapper;
import cn.fxl.pet.mapper.PetMapper;
import cn.fxl.pet.mapper.PetOnlineAuditLogMapper;
import cn.fxl.pet.service.IPetService;
import cn.fxl.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 业务实现类：
 */
@Service
public class PetServiceImpl extends BasicServiceImpl<Pet> implements IPetService {
   @Autowired
    private PetMapper petMapper;
    @Autowired
    private PetDetailMapper petDetailMapper;
    @Autowired
    private PetOnlineAuditLogMapper petOnlineAuditLogMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Override
    public void save(Pet pet) throws MessagingException {
        super.save(pet);
        if(pet.getPetDetail()!=null){
            pet.getPetDetail().setPet_id(pet.getId());
            petDetailMapper.save(pet.getPetDetail());
        }
    }

    @Override
    public void remove(Long id) {
        super.remove(id);
        petDetailMapper.deleteByPetid(id);
    }

    @Override
    public void update(Pet pet) {
        super.update(pet);
        if(pet.getPetDetail()!=null){
            PetDetail petDetail = pet.getPetDetail();
            petDetail.setPet_id(pet.getId());
            petDetailMapper.update(pet.getPetDetail());
        }
    }





    @Override
    public AjaxResult petOffssale(Long[] ids, HttpServletRequest request) {
        Map<String,Object> params = new HashMap<>();
        params.put("ids",ids);
        params.put("offsaletime",new Date());
        petMapper.offsale(params);
        return AjaxResult.me();
    }

    @Override
    public AjaxResult petOnsale(Long[] arr, HttpServletRequest request) {
        PetOnlineAuditLog petOnlineAuditLog = new PetOnlineAuditLog();
        AjaxResult ajaxResult = new AjaxResult();
        String returnMsg="操作成功";
        for (Long aLong : arr) {
            Pet pet = petMapper.getOne(aLong);
            if(!BaiduAiUtils.textCensor(pet.getName())){
                ajaxResult.setSuccess(false).setMsg("宠物名字违规");
                petOnlineAuditLog.setNote("宠物名字违规");
            }
            if(pet.getResources()!=null && !"".equals(pet.getResources().trim())){
                String resources = pet.getResources();
                String[] split = resources.split(",");
                String img;
                for(int i=0;i<split.length;i++){
                    img="http://123.207.27.208"+split[i];
                    if (!BaiduAiUtils.imgCensor(img)){
                        if(ajaxResult.getMsg().equals("宠物名字违规")){
                            ajaxResult.setSuccess(false).setMsg(ajaxResult.getMsg()+",宠物图片违规");
                            petOnlineAuditLog.setNote(ajaxResult.getMsg()+",宠物图片违规");
                        }else {
                            ajaxResult.setSuccess(false).setMsg("宠物图片违规");
                            petOnlineAuditLog.setNote("宠物图片违规");
                        }
                    }
                }
            }
            if(ajaxResult.getMsg().equals("操作成功")){
                pet.setState(1);
                petOnlineAuditLog.setPet_id(pet.getId());
                petOnlineAuditLog.setState(1);
                petOnlineAuditLog.setNote("操作成功");
                Logininfo logininfo = LoginContext.getLogininfo(request);
              Employee employee=  employeeMapper.getByLogininfoId(logininfo.getId());
                petOnlineAuditLog.setAudit_id(employee.getId());
                petOnlineAuditLogMapper.save(petOnlineAuditLog);
                pet.setOnsaletime(petOnlineAuditLog.getAudit_time());
                petMapper.update(pet);
                ajaxResult.setMsg("操作成功");
                returnMsg="操作成功";
            }else {
                pet.setState(0);
                petOnlineAuditLog.setPet_id(pet.getId());
                petOnlineAuditLog.setState(0);
                Logininfo logininfo = LoginContext.getLogininfo(request);
                Employee employee=  employeeMapper.getByLogininfoId(logininfo.getId());
                petOnlineAuditLog.setAudit_id(employee.getId());
                petOnlineAuditLogMapper.save(petOnlineAuditLog);
                petMapper.update(pet);
                returnMsg=ajaxResult.getMsg();
                ajaxResult.setMsg("操作成功");
            }

        }
        return ajaxResult.setMsg(returnMsg);
    }


}
